package chetan.VideoTest;


import chetan.VideoTest.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.WindowManager;
import android.widget.VideoView;

public class VideoTest extends Activity implements SurfaceHolder.Callback{    
    private VideoView mLeftVideo                       = null;
    private VideoView mRightVideo                       = null;
    private Menu mMenu                                 = null;
    
 
    private boolean isRunning                          = false;
    private final static String TAG                    = "VideoTest";
    
    private boolean mMenuItemSelected  				   = false;
    private int mMenuItem;
    private boolean mSurfaceChanged                    = false;
    private static boolean isDebug = true;
    
    //Debug methods
    public static String getLineInfo() {
        if(isDebug == false)
            return "";

        String result ="";
        try{
            StackTraceElement[] ste = new Throwable().getStackTrace();
		    if(ste.length <2)
		        return result;
		    
		    result = "[" + ste[1].getMethodName() +"] \n";

		    for(int i=1;i < ste.length;i++){
		        result += "  (" + i + ") " + ste[i].getFileName() + ", Func:" + ste[i].getMethodName() +", Line:" + ste[i].getLineNumber() + " \n";
		    }
        }catch(Exception e){};
        return result;
    }

    public static String getOneLineInfo() {
        if(isDebug == false)
            return "";
        String result = "";
        try{
            StackTraceElement[] ste = new Throwable().getStackTrace();
            

            if(ste.length <2)
                return result;
            
            result = "[" + ste[1].getMethodName() +"] \n";

            for(int i=1;i < ste.length && i < 3;i++){
                result += "  (" + i + ") " + ste[i].getFileName() + ", Func:" + ste[i].getMethodName() +", Line:" + ste[i].getLineNumber() + " \n";
            }
        }catch(Exception e){};
        return result;
    }

   @Override
   public void onCreate(Bundle savedInstanceState) {        
        super.onCreate(savedInstanceState);        
        Log.i(TAG, getLineInfo());

        setContentView(R.layout.videotest);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

         
        mLeftVideo = (VideoView) findViewById(R.id.leftView);
        mRightVideo = (VideoView) findViewById(R.id.rightView);
        mRightVideo.getHolder().addCallback(this);
        Log.d(TAG,"@@@@@@@@@START@@@@@@@");
        mRightVideo.getHolder().setFormat(0x400000);//setZOrderOnTop(true);
        Log.d(TAG,"@@@@@@@@@END@@@@@@@");
        mLeftVideo.setOnCompletionListener(mCompletionListener);
        mLeftVideo.setOnErrorListener(mErrorListener);

        mRightVideo.setOnCompletionListener(mCompletionListener);
        mRightVideo.setOnErrorListener(mErrorListener);
   }

    @Override
    public void onStart() {        
        super.onStart();
        Log.i(TAG, getLineInfo());
    }

    @Override
    public void onRestart() {        
        super.onRestart();
        Log.i(TAG, getLineInfo());
    }

    @Override
    public void onResume() {        
        super.onResume();
        Log.i(TAG, getLineInfo());
        //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        go();        
    }

    private void go(){
        Log.i(TAG, getLineInfo());

        if(isRunning==true)
        	return;
        try{
			    Uri uri = Uri.parse("/sdcard"+"/"+"The_International.3gp");
        	    mLeftVideo.setVideoURI(uri);
        	    mLeftVideo.start();
                               
                Uri uri2 = Uri.parse("/sdcard"+"/"+"clip.mp4");
                mRightVideo.setVideoURI(uri2);
                mRightVideo.start();

        } catch (Exception e) {
            new AlertDialog.Builder(this)                    
                    .setTitle("can not play this video")
                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            finish();                
                        }  
             }).show();      
        }

        
    }

    @Override
    public void onPause() {                
        super.onPause();
        Log.i(TAG, getOneLineInfo());
      
        mLeftVideo.pause();       
        mLeftVideo.setVideoURI(null);
        
        mRightVideo.pause();       
        mRightVideo.setVideoURI(null);
        android.os.Process.killProcess(android.os.Process.myPid());
        
    }

    @Override
    public void onStop() {        
        super.onStop();
        Log.i(TAG, getLineInfo());
    }

    @Override
    public void onDestroy() {        
        super.onDestroy();
        
        Log.i(TAG, getLineInfo());
    }
   
        
    private OnCompletionListener mCompletionListener =
        new OnCompletionListener() {
        public void onCompletion(MediaPlayer mp) {            
                Log.i(TAG, getOneLineInfo());
                mp.seekTo(0);                
                mp.start();                
        }
    };

    private OnErrorListener mErrorListener =
        new OnErrorListener() {
        public boolean onError(MediaPlayer mp, int framework_err, int impl_err) {
            finish();
            return true;
        }
    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        mMenu = menu;
        super.onCreateOptionsMenu(menu);
        createOptionsMenu();
        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        mMenu = menu;
        super.onPrepareOptionsMenu(menu);
        createOptionsMenu();
        return true;
    }

    private void createOptionsMenu() {
        mMenu.clear();
        mMenu.add(0, 0, 0, "Left Go BACK");
        mMenu.add(0, 1, 0, "Left Go FRONT");
    }

    //function to change the z order depending on the menu choice
    private void doItemSelected(int choice){
    	switch (choice) {
        case 0:
        	Log.d(TAG,"@@@@@@@ Left SENT BACK HERE - START @@@@@@@@@");        	
        	mLeftVideo.setZOrderOnTop(false);
        	mRightVideo.setZOrderOnTop(true);
        	/*Log.d(TAG,"@@@@ Calling setLayer explicitly START@@@");
        	mRightVideo.getHolder().getSurface().setLayer(1);
        	mLeftVideo.getHolder().getSurface().setLayer(0);
        	Log.d(TAG,"@@@@ Calling setLayer explicitly END@@@");*/
        	mMenuItem = 0;
        	Log.d(TAG,"@@@@@@@ Left SENT BACK HERE - END @@@@@@@@@");
            break;
        case 1:
        	Log.d(TAG,"@@@@@@@ Left SENT FRONT HERE - START @@@@@@@@@");
        	mMenuItem = 1;
        	//mRightVideo.setZOrderOnTop(false);
        	//mLeftVideo.setZOrderOnTop(true);
        	
        	/*Log.d(TAG,"@@@@ Calling setLayer explicitly START@@@");
        	mLeftVideo.getHolder().getSurface().setLayer(1);
        	mRightVideo.getHolder().getSurface().setLayer(0);
        	Log.d(TAG,"@@@@ Calling setLayer explicitly END@@@");
*/        	
        	Log.d(TAG,"@@@@@@@ Left SENT FRONT HERE - END @@@@@@@@@");
            break;
        }
    	mMenuItemSelected = false;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
    	mMenuItemSelected = true;
    	// if the surface has been created, go ahead and change the view. If not, wait for surface to change before changing the view
    	if (mSurfaceChanged)
    		doItemSelected(item.getItemId());
    	else{
    		switch (item.getItemId()) {
            case 0:
            	mMenuItem = 0;
                break;
            case 1:
            	mMenuItem = 1;
                break;
           }	
    	}
        return true;
    /*	switch (item.getItemId()) {
        case 0:
        	Log.d(TAG,"@@@@@@@ Left SENT BACK HERE - START @@@@@@@@@");        	
        	mLeftVideo.setZOrderOnTop(false);
        	mRightVideo.setZOrderOnTop(true);
        	
        	Log.d(TAG,"@@@@ Calling setLayer explicitly START@@@");
        	mRightVideo.getHolder().getSurface().setLayer(1);
        	mLeftVideo.getHolder().getSurface().setLayer(0);
        	Log.d(TAG,"@@@@ Calling setLayer explicitly END@@@");
        	
        	Log.d(TAG,"@@@@@@@ Left SENT BACK HERE - END @@@@@@@@@");
            break;
        case 1:
        	Log.d(TAG,"@@@@@@@ Left SENT FRONT HERE - START @@@@@@@@@");
        	
        	mRightVideo.setZOrderOnTop(false);
        	mLeftVideo.setZOrderOnTop(true);
        	
        	Log.d(TAG,"@@@@ Calling setLayer explicitly START@@@");
        	mLeftVideo.getHolder().getSurface().setLayer(1);
        	mRightVideo.getHolder().getSurface().setLayer(0);
        	Log.d(TAG,"@@@@ Calling setLayer explicitly END@@@");
        	
        	Log.d(TAG,"@@@@@@@ Left SENT FRONT HERE - END @@@@@@@@@");
            break;
        }
    	return true;*/
    }
    
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		Log.e(TAG,"surface changed");
		mSurfaceChanged = true;
		
		/*if (mMenuItemSelected){
			switch(mMenuItem){
			case 0:
				holder.setFormat(4194304);
				break;
			case 1:
				holder.setFormat(4194304);
				break;
			default:
					Log.d(TAG,"menuitem is weird: "+mMenuItem);
			}
			mMenuItemSelected = false;
		}*/
		// TODO Auto-generated method stub		
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		Log.e(TAG,"surface created: holder is " +holder);		
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		Log.e(TAG,"surface destroyed: holder is " + holder);
		// TODO Auto-generated method stub
		
	}
}